library(tidyverse)
library(rvest)
library(tidycensus)
# county level election results
counties_24<-read_csv('https://raw.githubusercontent.com/tonmcg/US_County_Level_Election_Results_08-24/refs/heads/master/2024_US_County_Level_Presidential_Results.csv')
# list of states by date of joining the union
page<-read_html('https://en.wikipedia.org/wiki/List_of_U.S._states_by_date_of_admission_to_the_Union')
join_date<-page|>
html_element(css='table')|>
html_table()|>
select(2:4)|>
mutate(datestring = str_extract(`Date(admitted or ratified)`, "([A-Z][a-z]+ [0-9]{1,2}, [0-9]{4})"),
date = mdy(datestring)
)
counties<-counties_24|>
left_join(join_date, by=join_by(state_name==State))|>
mutate(join_year = year(date) - 1787,
percent_gop = per_gop * 100
)|>
drop_na(join_year)
model<-lm(percent_gop ~ join_year, data=counties)